package com.groupbuying.customerconsumer.mq;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 支付中心消息消费MQ配置文件
 *
 * @Author Vincentxin
 * @Date 2019-05-10
 */
@Configuration
public class CustomerMQConsumer {

    @Autowired
    private CustomerListener customerListener;

    public static final Logger LOGGER = LoggerFactory.getLogger(CustomerMQConsumer.class);


    public CustomerMQConsumer() {
    }

    @Bean
    public Consumer getCustomerMQConsumer() {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.GROUP_ID, CustomerMQConfig.GROUP_ID);
        properties.put(PropertyKeyConst.AccessKey, CustomerMQConfig.ACCESS_KEY);
        properties.put(PropertyKeyConst.SecretKey, CustomerMQConfig.SECRET_KEY);
        properties.put(PropertyKeyConst.NAMESRV_ADDR, CustomerMQConfig.NAMESRV_ADDR);
        Consumer consumer = ONSFactory.createConsumer(properties);
        try {
            //订阅支付中心下所有支付回执的Tag
            consumer.subscribe(CustomerMQConfig.TOPIC, CustomerMQConfig.TAG_ALL, customerListener);
            consumer.start();
            LOGGER.info("用户中心消息消费者启动  groupId:{}", CustomerMQConfig.GROUP_ID);
        } catch (Exception var4) {
            LOGGER.error("用户中心消息消费者启动失败  groupId:{},错误原因:{}", CustomerMQConfig.GROUP_ID, var4.getMessage());
        }

        return consumer;
    }
}
